<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'MessageJsp.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <script>
           var ti=initTime();
           var request=getXMLHttpRequest();
           function initTime(){
               var date=new Date();
               var str=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
               return str;
           }
           
           function getXMLHttpRequest(){
              if(XMLHttpRequest)
                return new XMLHttpRequest();
               else
                 return new ActiveXObject("Microsoft.XMLHTTP");
           }
           
          function checkMessage(){
              var message=document.getElementById("message");
              if(message.value==""){
                 alert("消息不得为空！");
                     
              }else{
                
                /*  request.onreadystatechange=function(){
                   if(request.readyState==4){
                           if(request.status==200){
                             var text=request.responseText;
                             if(text.indexOf("success")!=-1){
                                
                                 getMessage();  
                             }else{
                                  var string="提示：发送失败\n";
                                   document.getElementById("textarea").appendChild(document.createTextNode(string));   
                             }         
                           }
                       }
                  }*/
                  request.open("get","servlet/MessageServlet?message="+message.value+"&method=send",true);
                  request.send(null);
              
              
              }
              
           }
           
           
           function getMessage(){
                request.onreadystatechange=function(){
                      if(request.readyState==4){
                           if(request.status==200){
                                var xml=request.responseXML;
                                var messages=xml.getElementsByTagName("message");
                                for(var i=messages.length-1;i>=0;i--){
                                     
                                     var from=messages[i].getElementsByTagName("from")
                                     var msg=messages[i].getElementsByTagName("content")
                                     var time=messages[i].getElementsByTagName("time")
                                     if(i==0)
                                       ti=time[0].innerHTML;
                                       var str=from[0].innerHTML+": "+msg[0].innerHTML+"   "+time[0].innerHTML+"\n";
                                      document.getElementById("textarea").appendChild(document.createTextNode(str));
                                    }
                       }
                  
                  }
                
                }
                request.open("get","servlet/MessageServlet?method=get&time="+ti,true);
                request.send(null);
           }
           
      setInterval("getMessage()", 1000);
    </script>
  </head>
  
  <body>
     <div>
       <form action="servlet/MessageServlet" method="post" onsubmit="checkMessage();return false">
       <input type="text"  name="message" id="message"/>
       <input type="submit" value="发送"/>
       </form>
     </div>
     <div>
<textarea rows="30px" cols="40px" id="textarea">
开始聊天：

</textarea>
    </div>
  </body>
</html>
